package com.mofang.powerdekorhelper.activity.order;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.adapter.OrderProductListAdapter;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewList;
import com.mofang.powerdekorhelper.model.OrderDetial;
import com.mofang.powerdekorhelper.model.ResultMessage;
import com.mofang.powerdekorhelper.persenter.OrderDetialPersenter;
import com.mofang.powerdekorhelper.utils.DialogUtils;
import com.mofang.powerdekorhelper.utils.LoadPrograss;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.OrderDetialView;
import com.mofang.powerdekorhelper.witget.MySwipeRefreshLayout;
import com.mofang.powerdekorhelper.witget.TitleBar;

import java.util.List;

import butterknife.BindView;

/**
 * Created by luofei on 2017/8/23 0023.
 * 订单详情
 */

public class OrderDetailActivity extends MvpActivity<OrderDetialView, OrderDetialPersenter> implements
        OrderDetialView,
        SwipeRefreshLayout.OnRefreshListener {

    public final static int ToDetailCode = 111;

    @BindView(R.id.orde_detail_refresh)
    MySwipeRefreshLayout refreshLayout;

    @BindView(R.id.orde_detail_titlebar)
    TitleBar mTitleBar;

    @BindView(R.id.orde_detail_code_tv)
    TextView ordeCodeTv;

    @BindView(R.id.orde_detail_state_tv)
    TextView ordeStateTv;

    @BindView(R.id.activity_name_tv)
    TextView activityNameTv;

    @BindView(R.id.activity_code_tv)
    TextView activityCodeTv;

    @BindView(R.id.orde_detail_customer_name)
    TextView customerNameTv;

    @BindView(R.id.orde_detail_customer_phone)
    TextView customerPhoneTv;

    @BindView(R.id.order_detial_addressee)
    TextView addresseeTv;

    @BindView(R.id.order_detial_consignee_phone)
    TextView consigneePhoneTv;

    @BindView(R.id.order_detial_address)
    TextView addressTv;

    @BindView(R.id.order_detial_time)
    TextView createTimeTv;

    @BindView(R.id.order_detail_advance_deposit)
    TextView depositPriceTv; //预付定金

    /*@BindView(R.id.order_detail_user_card_tv)
    TextView userCardTv; // 是否使用活动卡*/

    @BindView(R.id.order_detail_payment_method)
    TextView paymentMethodTv; // 付款方式

    @BindView(R.id.order_detail_prize_content)
    TextView prizeContentTv;

    /*@BindView(R.id.order_detail_luck_dan)
    TextView luckDanTv;

    @BindView(R.id.order_detail_luck_draw)
    TextView luckdDrawTv;*/

    @BindView(R.id.order_detail_full_gift)
    TextView fullGiftTv;

    @BindView(R.id.order_detail_paid_in_amount)
    TextView paidInAmountTv;

    /*@BindView(R.id.order_detial_remark_edit)
    EditText remarkTv;*/

    @BindView(R.id.order_detial_explain_content)
    TextView explainContentTv;

    @BindView(R.id.exclusive_clerk_name)
    TextView exclusiveClerkName;

    @BindView(R.id.exclusive_clerk_phone)
    TextView exclusiveClerkPhoneTv;

   /* @BindView(R.id.print_clerk_name)
    TextView printClerkNameTv;

    @BindView(R.id.print_clerk_phone)
    TextView printClerkPhoneTv;*/

    @BindView(R.id.order_detial_product_list)
    BaseRecycleViewList productList;

    @BindView(R.id.order_detail_user_card_tv)
    TextView userCardTv;

    @BindView(R.id.order_detial_remark_tv)
    TextView remarkTv;

    @BindView(R.id.order_detail_continer1)
    LinearLayout continer1;

    @BindView(R.id.order_detail_continer2)
    LinearLayout continer2;

    OrderProductListAdapter adapter;

    private LoadPrograss loadPrograss;

    private String orderCode;

    private Integer orderId;
    private int activityId;
    private int customerId;

    private String customerName;
    private String customerPhone;

    private OrderDetial orderDetial;

    @Override
    public void initView() {

        inflateLayout(R.layout.activity_order_detial_layout);
        initTitlebar();
        loadPrograss = new LoadPrograss(this);
        refreshLayout.setOnRefreshListener(this);

        productList.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
    }

    @Override
    public void initData() {

        orderId = getIntent().getIntExtra("OrderId", -1);

        orderCode = getIntent().getStringExtra("OrderCode");
        activityId = getIntent().getIntExtra("ActivityId", -1);
        customerId = getIntent().getIntExtra("CustomerId", -1);

        customerName = getIntent().getStringExtra("CustomerName");
        customerPhone = getIntent().getStringExtra("CustomerPhone");

        presenter.getOrderDetial(orderId);

        // presenter.getCheckOrder(ParamsUtils.getcheckOrderParams(orderCode, activityId, shopId, customerId));
    }

    @Override
    public void setOrderDetial(OrderDetial orderDetial) {

        OrderDetial.Result result = orderDetial.getResult();

        ordeCodeTv.setText(result.getOrderCode());
        String ordeCodeStr = "";
        if (result.getOrderState() != null) {
            switch (result.getOrderState()) {
                case 1:
                    ordeCodeStr = "物流";
                    break;
                case 2:
                    ordeCodeStr = "安装";
                    break;
                case 3:
                    ordeCodeStr = "售后";
                    break;
                case 4:
                    ordeCodeStr = "关闭";
                    break;
            }
        }
        ordeStateTv.setText(ordeCodeStr);
        activityCodeTv.setText(result.getActivityCode());
        customerNameTv.setText(result.getCustomerName());
        customerPhoneTv.setText(result.getCustomerPhone());
        addresseeTv.setText("收件人：" + result.getConsignee());
        addressTv.setText("收货地址：" + result.getConsigneeAddress());
        consigneePhoneTv.setText("电话： " + result.getConsigneePhone());
        createTimeTv.setText("下单日期: " + result.getCreateTime());
        if (result.getDepositPrice() != 0) {
            depositPriceTv.setText("已付总额: " + result.getDepositPrice());
        } else {
            depositPriceTv.setText("已付总额: " + "无");
        }

        if (result.getIsUseActivityCard() != null && !result.getIsUseActivityCard().equals("")) {
            userCardTv.setText(result.getIsUseActivityCard());
        } else {
            userCardTv.setText("无");
        }

        if (result.getPayStatus() != null && !result.getPayStatus().equals("")) {
            paymentMethodTv.setText("付款方式: " + result.getPayStatus());
        } else {
            paymentMethodTv.setVisibility(View.GONE);
        }

        if (result.getPrize() != null && !result.getPrize().equals("")) {
            prizeContentTv.setText("奖品内容: " + result.getPrizes());
        } else {
            prizeContentTv.setVisibility(View.GONE);
        }

        if (result.getRealPayPrice() != 0) {
            paidInAmountTv.setText("本次实收金额: " + result.getRealPayPrice());
        } else {
            paidInAmountTv.setText("本次实收金额: " + "无");
        }

        if (result.getSellerName() != null) {
            exclusiveClerkName.setText("专属店员姓名: " + result.getSellerName());
        } else {
            exclusiveClerkName.setText("专属店员姓名: " + "无");
        }

        if (result.getSellerPhone() != null) {
            exclusiveClerkPhoneTv.setText("专属店员电话: " + result.getSellerPhone());
        } else {
            exclusiveClerkPhoneTv.setText("专属店员电话: " + "无");
        }

        if (result.getPrize() != null && !result.getPrize().equals("")) {
            fullGiftTv.setText("满赠礼品: " + result.getPrize());
        } else {
            fullGiftTv.setVisibility(View.GONE);
        }

        activityNameTv.setText(orderDetial.getResult().getActivityName());
        remarkTv.setText(result.getRemarks());
        explainContentTv.setText(result.getExplain());

        // 订单产品列表
        adapter = new OrderProductListAdapter(orderDetial.getResult().getProductInfoList(), this, R.layout.order_product_list_item_layout);
        productList.setAdapter(adapter);

        // 动态显示砸蛋及抽奖
        continer1.removeAllViews();
        List<OrderDetial.ActivityDetail> activityDetails = orderDetial.getResult().getActivityDetail();

        /*if (activityDetails.size() > 0) {

            for (OrderDetial.ActivityDetail activityDetail : activityDetails) {
                TextView textView = new TextView(this);
                textView.setText(activityDetail.getName() + ": " + activityDetail.getNum() + "次");
                continer1.addView(textView);
            }
        } else {
            continer1.setVisibility(View.GONE);
        }*/

        if (activityDetails != null && activityDetails.size() > 0) {

            int i = 0;
            for (OrderDetial.ActivityDetail activityDetail : activityDetails) {
                TextView textView = new TextView(this);

                if (i == 0) {

                    if (activityDetail.getNum() == null || activityDetail.getNum().equals("")) {

                        textView.setText(activityDetail.getName() + ": " + "0" + "次");
                    } else {
                        textView.setText(activityDetail.getName() + ": " + activityDetail.getNum() + "次");
                    }

                } else {
                    if (activityDetail.getNum() == null || activityDetail.getNum().equals("")) {

                        textView.setText("       " + activityDetail.getName() + ": " + "0" + "次");
                    } else {
                        textView.setText("       " + activityDetail.getName() + ": " + activityDetail.getNum() + "次");
                    }

                }
                continer1.addView(textView);
                i++;
            }
            i = 0;
        } else {
            continer1.setVisibility(View.GONE);
        }

        hideProgress();
    }

    @Override
    public void setCheckOrder(OrderDetial orderDetial) {

        this.orderDetial = orderDetial;

        OrderDetial.Result result = orderDetial.getResult();

        ordeCodeTv.setText(result.getOrderCode());
        ordeStateTv.setText(result.getOrderState());
        activityCodeTv.setText(result.getActivityCode());
        customerNameTv.setText(customerName);
        customerPhoneTv.setText(customerPhone);
        addresseeTv.setText("收件人：" + result.getConsignee());
        addressTv.setText("收货地址：" + result.getConsigneeAddress());
        consigneePhoneTv.setText(result.getConsigneePhone());
        createTimeTv.setText("下单日期: " + result.getCreateTime());
        // depositPriceTv.setText("预付定金: " + result.getDepositPrice());
        userCardTv.setText(result.getIsUseActivityCard());

        if (result.getPayStatus() != null && !result.getPayStatus().equals("")) {
            paymentMethodTv.setText("付款方式: " + result.getPayStatus());
        } else {
            paymentMethodTv.setVisibility(View.GONE);
        }

        if (result.getPrizes() != null && !result.getPrizes().equals("")) {
            prizeContentTv.setText("奖品内容: " + result.getPrizes());
        } else {
            prizeContentTv.setVisibility(View.GONE);
        }

        if (result.getPrize() != null && !result.getPrize().equals("")) {
            fullGiftTv.setText("满赠礼品: " + result.getPrize());
        } else {
            fullGiftTv.setVisibility(View.GONE);
        }

        paidInAmountTv.setText("本次实收金额: " + result.getRealPayPrice());
        remarkTv.setText(result.getRemarks());
        explainContentTv.setText(result.getExplain());
        exclusiveClerkName.setText("专属店员姓名: " + result.getSellerName());
        exclusiveClerkPhoneTv.setText("专属店员电话: " + result.getSellerPhone());
        /*printClerkNameTv.setText("打单员姓名: " + result.getTypistName());
        printClerkPhoneTv.setText("打单员姓名: " + result.getTypistPhone());*/
        activityNameTv.setText(orderDetial.getResult().getActivityName());

        // 订单产品列表
        adapter = new OrderProductListAdapter(orderDetial.getResult().getProductInfoList(), this, R.layout.order_product_list_item_layout);
        productList.setAdapter(adapter);


        // 动态显示砸蛋及抽奖
        continer1.removeAllViews();
        List<OrderDetial.Num> activityDetails = orderDetial.getResult().getNum();

        if (activityDetails != null && activityDetails.size() > 0) {

            int i = 0;
            for (OrderDetial.Num activityDetail : activityDetails) {
                TextView textView = new TextView(this);

                if (i == 0) {

                    if (activityDetail.getNum() == null || activityDetail.getNum().equals("")) {

                        textView.setText(activityDetail.getName() + ": " + "0" + "次");
                    } else {
                        textView.setText(activityDetail.getName() + ": " + activityDetail.getNum() + "次");
                    }

                } else {
                    if (activityDetail.getNum() == null || activityDetail.getNum().equals("")) {

                        textView.setText("       " + activityDetail.getName() + ": " + "0" + "次");
                    } else {
                        textView.setText("       " + activityDetail.getName() + ": " + activityDetail.getNum() + "次");
                    }

                }
                continer1.addView(textView);
                i++;
            }
            i = 0;
        } else {
            continer1.setVisibility(View.GONE);
        }

        // 动态显示满赠礼品
        continer2.removeAllViews();
        hideProgress();
    }

    @Override
    public void setCommitOrder() {
        hideProgress();
        toast("提交成功");
        finish();
    }

    @Override
    public void onRefresh() {
        presenter.getOrderDetial(orderId);

        //presenter.getCheckOrder(ParamsUtils.getcheckOrderParams(orderCode, activityId, shopId, customerId));
    }

    @Override
    public void showProgress() {
        loadPrograss.show();
    }

    @Override
    public void hideProgress() {
        loadPrograss.hide();
        refreshLayout.hide();
        dialogUtils.hideLoadingDialog();
    }

    @Override
    public void toast(CharSequence s) {
        T.showShort(this, s);
    }

    @Override
    public void toast(int id) {
        T.showShort(this, getResources().getString(id));
    }

    @Override
    public void showNullLayout() {
        hideProgress();
        toast(R.string.no_data);
    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {

    }

    @Override
    public void hideErrorLayout() {

    }

    @Override
    public void onError(String errorInfo) {
        hideProgress();
    }

    @Override
    public OrderDetialPersenter initPresenter() {
        return new OrderDetialPersenter();
    }

    private DialogUtils dialogUtils;

    private void initTitlebar() {

        dialogUtils = DialogUtils.getInstance();

        initTitleBarWithback(mTitleBar, R.string.order_detial);
        mTitleBar.setActionTextColor(getResources().getColor(R.color.fontcolordeep));
       /* mTitleBar.addAction(new TitleBar.TextAction("提交") {
            @Override
            public void performAction(View view) {
                dialogUtils.createLoadingDialog(OrderDetailActivity.this, "正在提交请稍候");
                createOrder();
            }
        });*/
    }

    // 创建订单
   /* private void createOrder() {
        Integer sellerId = SharedUtils.getInstance(this).getUserID();
        double realPayPrice = orderDetial.getResult().getRealPayPrice();
        double orderPrice = orderDetial.getResult().getOrderPrice();
        String remarks = orderDetial.getResult().getRemarks();
        String consignee = orderDetial.getResult().getConsignee();
        String consigneePhone = orderDetial.getResult().getConsigneePhone();
        String consigneeAddress = orderDetial.getResult().getConsigneeAddress();

        double depositPrice = orderDetial.getResult().getDepositPrice();
        String isUseActivityCard = userCardTv.getText().toString();
        String payStatus = orderDetial.getResult().getPayStatus();
        String prize = orderDetial.getResult().getPrize();
        Integer typistId = SharedUtils.getInstance(this).getUserID();
        // 产品参数
        List<OrderDetial.ProductInfoList> productInfoLists = orderDetial.getResult().getProductInfoList();
        // 奖品参数
        List<OrderDetial.Num> num = orderDetial.getResult().getNum();

        presenter.getCreateOrder(ParamsUtils.getCommitOrderParams(orderCode, shopId, sellerId, orderPrice, customerId, consignee, consigneePhone, consigneeAddress,
                realPayPrice, remarks, depositPrice, isUseActivityCard, payStatus, prize, activityId, typistId, productInfoLists, num));
    }*/

    @Override
    public void setUpdateOrder(ResultMessage resultMessage) {

    }
}
